package eop.project

import java.util.Map;

class LoadActivityDatabase {
	
	void readAndSaveActivityDataFromWeb(activityXmlService) {
		Map activityList = [:]
		def txt = activityXmlService.html2Xml()
		def parser = new org.cyberneko.html.parsers.SAXParser()
		parser.setFeature('http://xml.org/sax/features/namespaces', false)
		def page = new XmlSlurper(parser).parseText(txt)
		
		def tr = page.depthFirst().findAll {
			it != null && it.name() == 'PRE'
		}.each{
			println "Parsing VACC"
			parseVacc(it.toString())
		}
		
		println "tr " + tr.size()
	}
	
	Map parseVacc(String txt){
		if(txt==null || txt.length()<=0){
			return
		}
		
		def dtg = txt =~ /Received.*/
		def vaac = txt =~ /VAAC:.*/
		def volcano = txt =~ /VOLCANO:.*/
		def advisory = txt =~ /ADVISORY NR:.*/
		def source  = txt =~ /INFO SOURCE:.*/
		def erruption = txt =~ /ERUPTION DETAILS:.*/
		
		int counter = 0
		Map vacList = [:]
		Map volList = [:]
		//		println "dtg " + dtg.size()
		//		println "vaac " + vaac.size()
		//		println "volcano " + volcano.size()
		//		println "advisory " + advisory.size()
		//		println "source " + source.size()
		//		println "erruption " + erruption.size()
		
		dtg.each{
			volList = [:]
			def mat = it.split (" ")
			//			print "dtg: $it"
			volList["DTG"] = mat[5] + " " +mat[3]
			mat = vaac[counter] =~ /\s\w+/
			volList["VAAC"] = mat[0]
			//			println  volcano[counter]
			mat = volcano[counter].split("  ")
			if(mat.size()<3 || mat.size()>4){
				mat=volcano[counter].replaceAll(/  /, "").split(" ")
				println mat.length
				volList["VOLCANO-NAME"] = mat[mat.length - 2]
				volList["VOLCANO-REF"] = mat[mat.length - 1]
			}
			else{
				println mat
				volList["VOLCANO-NAME"] = mat[1]
				volList["VOLCANO-REF"] = mat[2]
			}
			mat = advisory[counter].split(" ")
			println "adv: " + mat
			volList["ADVISORY"] = mat[mat.size()-1]
			mat = source[counter] =~ /:(.*)/
			volList["SOURCE"] = mat[0][0].substring(1,mat[0][0].length())
			mat = erruption[counter] =~ /:(.*)/
			volList["ERRUPTION"] = (mat[0][0] != null && mat[0][0].length()>3) ? mat[0][0].substring(1,mat[0][0].length()) : ""
			println volList
			vacList["$counter"] = volList
			counter++
		}
		if(vacList!=null && vacList.size()> 0)
			saveActivity(vacList)
	}
	
	void saveActivity(Map aList){
		Volcano vol = null
		/*
		 volList["DTG"] = mat[5] + ":" +mat[3]
		 volList["VAAC"] = vaac[counter] =~ /\s\w+/
		 volList["VOLCANO-NAME"] = mat[1]
		 volList["VOLCANO-REF"] = mat[2]
		 volList["ADVISORY"] = mat[1]
		 volList["SOURCE"] = mat[0][0].substring(2,mat[0][0].length())
		 volList["ERRUPTION"] = mat[0][0].substring(3,mat[0][0].length())
		 */
		println "aList " + aList.size()
		aList.each{ k,activityMap ->
			String name = activityMap["VOLCANO-NAME"].replaceAll(" ","").toUpperCase()
			vol = Volcano.findByVolcanoName(name)
			if(vol){
				vol.volcanicRef = activityMap["VOLCANO-REF"]
				vol.save(flush:true)
				println "Found " +vol				
				
				//def errs = Eruption.createCriteria().list(max:2,sort:'eventDate',order:'desc') 
				 
				
				if(vol.eruptions != null && vol.eruptions.size() > 0){
										
					Eruption err 
					vol.eruptions.each { err = it}
					
					Activity activity = Activity.findByAdvisory(activityMap["ADVISORY"]);
					if(!activity){
						activity = new Activity()
						activity.dated = activityMap["DTG"]
						activity.vaac = activityMap["VAAC"]
						activity.advisory = activityMap["ADVISORY"]
						activity.source = activityMap["SOURCE"]
						activity.details = activityMap["ERRUPTION"]
						activity.eruption = err
						activity.validate()
						if(activity.hasErrors()){
							activity.errors.each{ println "Error : $it" }
						}
						
						activity.save(flush:true);					
						err.addToActivities(activity).save(flush:true);
						
						println "Activity saved " + activity
					}
					else{
						println "Activity found " + activity
					}
					
					//println "Err found " + errs.size()
				}
				else
					println "Err not found "
			}
			else
				println "Not Found " + activityMap["VOLCANO-NAME"]
		}
	}
}
